package com.csst.business.dao.mysql.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.csst.business.dao.INewsDao;
import com.csst.business.dao.IPhotoDao;
import com.csst.business.dao.TestDao;
import com.csst.business.model.BOnline;
import com.csst.business.model.TNews;
import com.csst.business.model.TPhotoGroup;
import com.csst.business.model.TPhotos;
import com.csst.business.model.TTrUser;
import com.csst.popular.bean.message.NewsMessage.News;

public class PhotoDaoImpl extends BaseDaoImpl implements IPhotoDao {

	@Override
	public int getOrderTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TPhotoGroup t  order by t.GCreatetime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}


	@Override
	public List<TPhotoGroup> getPhotoGroupList(int startNum, int endNum) {
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from TPhotoGroup t  order by t.GCreatetime desc";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<TPhotoGroup> list=q.list();
		session.flush();
		session.clear();
		return list;
	}


	@Override
	public void saveGroup(TPhotoGroup photog) {
		Session session = getSessionFactory().getCurrentSession();
		session.save(photog);
		session.flush();
		session.clear();
	}


	@Override
	public void updateGroup(TPhotoGroup photog) {
		Session session = getSessionFactory().getCurrentSession();
		session.update(photog);
		session.flush();
		session.clear();
		
	}


	@Override
	public void delteGroup(String gid) {
		Session session=getSessionFactory().getCurrentSession();
		String hql1 = "delete from TPhotos t where t.TPhotoGroup.id = '"+gid+"'";
		String hql2=" delete from TPhotoGroup t   where t.id='"+gid+"'";
		session.createQuery(hql1).executeUpdate();
		session.createQuery(hql2).executeUpdate();
		session.flush();
		session.clear();
	}


	@Override
	public TPhotoGroup getPhotoGById(String id) {
		Session session = getSessionFactory().getCurrentSession();
		TPhotoGroup photog=(TPhotoGroup)session.get(TPhotoGroup.class, id);
		return photog;
	}


	@Override
	public void savePhoto(TPhotos photo) {
		Session session = getSessionFactory().getCurrentSession();
		session.save(photo);
		session.flush();
		session.clear();
		
	}


	@Override
	public List<TPhotos> getPhotosBypgId(String gid, int startNum, int endNum) {
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from TPhotos t where t.TPhotoGroup.id='"+gid+"' order by t.PUpdate desc";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<TPhotos> list=q.list();
		session.flush();
		session.clear();
		return list;
	}


	@Override
	public int getPhotoTotle(String gid) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TPhotos t where t.TPhotoGroup.id='"+gid+"' order by t.PUpdate desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}


	@Override
	public void deltephoto(String gid) {
		Session session=getSessionFactory().getCurrentSession();
		String hql1 = "delete from TPhotos t where t.id = '"+gid+"'";
		session.createQuery(hql1).executeUpdate();
		session.flush();
		session.clear();
		
	}


	@Override
	public TPhotos getPhotoById(String gid) {
		Session session = getSessionFactory().getCurrentSession();
		TPhotos photog=(TPhotos)session.get(TPhotos.class, gid);
		return photog;
	}


	@Override
	public boolean hasChildPhoto(String gid) {
		boolean flag = false;
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from TPhotos t where t.TPhotoGroup.id='"+gid+"' order by t.PUpdate desc";
		Query q = session.createQuery(sql);
		List<TPhotos> list=q.list();
		if(list.size()>0){
			flag = false;
		}else{
			flag = true;
		}
		session.flush();
		session.clear();
		return flag;
	}

	

}
